package mega.awesome.assignment3;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;

public class CompassActivity extends Activity {
	
	private static SensorManager sensorManager;
	private Sensor sensorMagnetic, sensorOrientation;
	private TextView xText, yText, zText, azimuthText, pitchText, rollText;
	private File logFile;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_compass);
		
		sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
		sensorMagnetic = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
		sensorOrientation = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
		
		if (sensorMagnetic != null && sensorOrientation != null) {
			sensorManager.registerListener(magneticListener, sensorMagnetic, SensorManager.SENSOR_DELAY_FASTEST);
			sensorManager.registerListener(orientationListener, sensorOrientation, SensorManager.SENSOR_DELAY_FASTEST);
			
			
			xText = (TextView)findViewById(R.id.xvalue);
			yText = (TextView)findViewById(R.id.yvalue);
			zText = (TextView)findViewById(R.id.zvalue);
			azimuthText = (TextView)findViewById(R.id.azimuthvalue);
			pitchText = (TextView)findViewById(R.id.pitchvalue);
			rollText = (TextView)findViewById(R.id.rollvalue);
			
		} else {
			Log.e("CompassActivity", "Registrered for MAGNETIC_FIELD & ORIENTATION sensor");
			finish(); // shut down the activity
		}
		
		logFile = new File("sdcard/data_compass.txt");
		
		if (!logFile.exists()) {
			try {
				logFile.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	private SensorEventListener magneticListener = new SensorEventListener() {
		
		@Override
		public void onSensorChanged(SensorEvent event) {
			// TODO Auto-generated method stub
			float x = event.values[0];
			float y = event.values[1];
			float z = event.values[2];
			
			xText.setText("" + x);
			yText.setText("" + y);
			zText.setText("" + z);
		}
		
		@Override
		public void onAccuracyChanged(Sensor sensor, int accuracy) {
			// TODO Auto-generated method stub
		}
	};
	
	private SensorEventListener orientationListener = new SensorEventListener() {
		
		@Override
		public void onSensorChanged(SensorEvent event) {
			
			String line = "";
			Date date = new Date();
			float azimuth = event.values[0];
			float pitch = event.values[1];
			float roll = event.values[2];
			
			Log.i("COMPASS ACTIVITY", "onSensorChanged OrientationListener");
			
			azimuthText.setText("" + azimuth);
			pitchText.setText("" + pitch);
			rollText.setText("" + roll);
			
			line = date.getTime() + ";" + azimuth;
			
			try {
				BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));
				buf.append(line);
				buf.newLine();
				buf.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
		
		@Override
		public void onAccuracyChanged(Sensor sensor, int accuracy) {
			// TODO Auto-generated method stub
		}
	};

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_compass, menu);
		return true;
	}

}
